//
//  ZYPersonManagerAddPersonTableViewController.m
//  ZYOils
//
//  Created by 段新瑞 on 2019/9/30.
//  Copyright © 2019 Mr.Xr. All rights reserved.
//

#import "ZYPersonManagerAddPersonTableViewController.h"
#import "ZYRequestAddPersonModel.h"

@interface ZYPersonManagerAddPersonTableViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;

@property (weak, nonatomic) IBOutlet UITextField *phoneNumTextField;

@property (weak, nonatomic) IBOutlet UITextField *idCardTextField;

@property (weak, nonatomic) IBOutlet UITextField *sexTextField;

@property (nonatomic, strong) ZYRequestAddPersonModel *requestModel;

@end

@implementation ZYPersonManagerAddPersonTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configNavigationBar];
    
    [self.nameTextField becomeFirstResponder];
    
    RAC(self.requestModel, userName) = self.nameTextField.rac_textSignal;
    RAC(self.requestModel, phone) = self.phoneNumTextField.rac_textSignal;
    RAC(self.requestModel, idCard) = self.idCardTextField.rac_textSignal;
    
    [LBLimitInput limitPhoneInputView:self.phoneNumTextField];
    
    if (self.state == PersonStateChange) {
        self.nameTextField.text = self.model.userName;
        self.requestModel.userName = self.model.userName;
        
        self.sexTextField.text = self.model.gender;
        self.requestModel.gender = [self.model.gender isEqualToString:@"男"] ? @"0": @"1";
        
        self.phoneNumTextField.text = self.model.phone;
        self.requestModel.phone = self.model.phone;
        
        self.idCardTextField.text = self.model.idCard;
        self.requestModel.idCard = self.model.idCard;
        
        self.requestModel.userId = self.model.userId;
    }
}

- (IBAction)confirmBtnClick:(id)sender {
    [self.view endEditing:YES];
    if ([LBLimitInput isEmpty:self.requestModel.userName]) {
        [ZYHud showTipsText:@"请输入姓名"];
        return;
    }
    
    if ([LBLimitInput isEmpty:self.requestModel.phone]) {
        [ZYHud showTipsText:@"请输入手机号"];
        return;
    }
    
    if (![LBLimitInput isValidPhoneNum:self.requestModel.phone]) {
        [ZYHud showTipsText:@"手机号输入有误"];
        return;
    }
    
    if ([LBLimitInput isEmpty:self.requestModel.gender]) {
        [ZYHud showTipsText:@"请选择性别"];
        return;
    }
    
    if ([LBLimitInput isEmpty:self.requestModel.idCard]) {
        [ZYHud showTipsText:@"请输入身份证号"];
        return;
    }
    
    if (![LBLimitInput isValidIdCard:self.requestModel.idCard]) {
        [ZYHud showTipsText:@"身份证号输入有误"];
        return;
    }
    
    
    [ZYHud showLoadingText:@""];
    if (self.state == PersonStateAdd) {
        /// 添加
        self.requestModel.userRole = @"2";
        self.requestModel.companyId = ZYUserInfoModel.sharedInfoModel.userModel.companyId;
        self.requestModel.userId = ZYUserInfoModel.sharedInfoModel.userModel.userId;
        [[DataManager manager] postAddPersonParam:[self.requestModel mj_keyValues] block:^(XRCommonModel *model, NSError *error) {
            [ZYHud hideHud];
            if (model.isSuccess) {
                [self.subject sendNext:@"add"];
                [ZYHud showSuccessText:@"店员添加成功"];
                @weakify(self)
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    @strongify(self)
                    [self.navigationController popViewControllerAnimated:YES];
                });
            } else {
                if (![LBLimitInput isEmpty:model.error]) {
                    [ZYHud showErrorText:model.error];
                }
            }
        }];
    } else {
        /// 修改
        [[DataManager manager] postUpdatePersonParam:[self.requestModel mj_keyValues] block:^(XRCommonModel *model, NSError *error) {
            [ZYHud hideHud];
            if (model.isSuccess) {
                [ZYHud showSuccessText:@"店员信息修改成功"];
                @weakify(self)
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    @strongify(self)
                    self.model.userName = self.requestModel.userName;
                    self.model.gender = [self.requestModel.gender isEqualToString:@"0"] ? @"男": @"女";
                    self.model.idCard = self.requestModel.idCard;
                    self.model.phone = self.requestModel.phone;
                    [self.subject sendNext:@"change"];
                    [self.navigationController popViewControllerAnimated:YES];
                });
            } else {
                if (![LBLimitInput isEmpty:model.error]) {
                    [ZYHud showErrorText:model.error];
                }
            }
        }];
    }
}


- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}


- (void)configNavigationBar {
    
    self.navigationController.navigationBar.barTintColor = CustomNavBarBackgroundColor;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont systemFontOfSize:17]};
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (UIBarButtonItem *)rt_customBackItemWithTarget:(id)target
                                          action:(SEL)action {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"navigation_back_white"] forState:UIControlStateNormal];
    [button sizeToFit];
    [button setFrame:CGRectMake(0, 0, 44, 44)];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}


- (IBAction)sexButtonClick:(id)sender {
    [self.view endEditing:YES];
    UIAlertController *alertController = [[UIAlertController alloc] init];
    @weakify(self)
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *takePicture = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self)
        self.sexTextField.text = action.title;
        self.requestModel.gender = @"0";
    }];
    UIAlertAction *choosePhoto = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self)
        self.sexTextField.text = action.title;
        self.requestModel.gender = @"1";
        
    }];
    [alertController addAction:cancel];
    [alertController addAction:takePicture];
    [alertController addAction:choosePhoto];
    [self presentViewController:alertController animated:YES completion:nil];
}


- (ZYRequestAddPersonModel *)requestModel {
    if (!_requestModel) {
        _requestModel = [[ZYRequestAddPersonModel alloc] init];
    }
    return _requestModel;
}

- (RACSubject *)subject {
    if (!_subject) {
        _subject = [RACSubject subject];
    }
    return _subject;
}

@end
